package hsp.admin.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hsp.admin.api.ApiResult;
import hsp.admin.app.base.dto.UsernamePassword;
import hsp.admin.app.base.info.RoleInfo;
import hsp.admin.app.base.po.UserPO;
import hsp.admin.app.base.service.IMenuService;
import hsp.admin.app.base.service.IRoleService;
import hsp.admin.app.base.service.IUserService;
import hsp.admin.app.base.info.MenuInfo;
import hsp.admin.app.base.info.UserInfo;
import hsp.admin.app.system.info.CodeInfo;
import hsp.admin.cache.CodeCache;
import hsp.admin.interceptor.UserIdInterceptor;
import hsp.admin.util.SessionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "BaseController", description = "基础信息")
@RestController
@RequestMapping("base")
public class BaseController {

    private final SessionUtil sessionUtil;

    private final IUserService userService;

    private final IRoleService roleService;

    private final IMenuService menuService;

    @Autowired
    public BaseController(
        SessionUtil sessionUtil,
        IUserService userService,
        IRoleService roleService,
        IMenuService menuService
    ){
        this.sessionUtil = sessionUtil;
        this.userService = userService;
        this.roleService = roleService;
        this.menuService = menuService;
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public ApiResult<Long> login(@RequestBody UsernamePassword usernamePassword){
        LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPO::getUsername, usernamePassword.getUsername());
        wrapper.eq(UserPO::getPassword, usernamePassword.getPassword());
        UserPO userPO = userService.getOne(wrapper);
        Assert.notNull(userPO, "账号或密码错误");
        return ApiResult.success(userPO.getId(), "登录成功");
    }

    @Operation(summary = "登出")
    @PostMapping("logout/{id}")
    public ApiResult<?> logout(@PathVariable("id") long id){
        UserIdInterceptor.removeUserId(id);
        return ApiResult.success("登出成功");
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("userinfo")
    public ApiResult<UserInfo> getUserInfo(){
        UserInfo userInfo = userService.fetchInfo();
        return ApiResult.success(userInfo);
    }

    @Operation(summary = "获取角色信息")
    @GetMapping("roleinfo")
    public ApiResult<List<RoleInfo>> getRoleInfo(){
        List<RoleInfo> roleInfoList = roleService.fetchInfo();
        return ApiResult.success(roleInfoList);
    }

    @Operation(summary = "获取菜单信息")
    @GetMapping("menuinfo/{roleId}")
    public ApiResult<List<MenuInfo>> getMenuInfo(@PathVariable("roleId") long roleId){
        List<MenuInfo> menuInfoList = menuService.fetchInfo(roleId);
        return ApiResult.success(menuInfoList);
    }

    @Operation(summary = "获取词典信息")
    @GetMapping("codeinfo")
    public ApiResult<List<CodeInfo>> getCodeInfo(){
        List<CodeInfo> codeInfoList = CodeCache.getCodeInfoList();
        return ApiResult.success(codeInfoList);
    }

}
